package com.epam.my.Pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;

public class LoginPage extends InternalPage {
	@FindBy(id = "login")
	public WebElement loginField;
	@FindBy(id = "domain")
	public WebElement domainNameField;
	@FindBy(id = "password")
	public WebElement passwordField;
	@FindBy(id = "longSession")
	public WebElement checkboxRememberMe;
	@FindBy(name = "profile.send")
	public WebElement loginButton;

	@Override
	public boolean isOnThisPage() {
		return loginField.isDisplayed();
	}

	@Override
	public void tryToOpen() {
		MyPageFactory.getPage(driver, InternalPage.class).logOut();
	}

	public MailPage loginWithValidCredentials(String userName,
			String domainName, String login) {
		loginField.clear();
		loginField.sendKeys(userName);
		new Select(domainNameField).selectByVisibleText(domainName);
		passwordField.clear();
		passwordField.sendKeys(login);
		checkboxRememberMe.click();
		loginButton.click();
		return MyPageFactory.getPage(driver, MailPage.class);
	}

	public LoginPage loginWithwrongCredentials(String userName,
			String domainName, String login) {
		loginField.clear();
		loginField.sendKeys(userName);
		new Select(domainNameField).selectByVisibleText(domainName);
		passwordField.clear();
		passwordField.sendKeys(login);
		checkboxRememberMe.click();
		loginButton.click();
		return MyPageFactory.getPage(driver, LoginPage.class);
	}

}
